package sjg.practice.WBtest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil1 {
	public static final String DRIVER = "com.mysql.jdbc.Driver";
	public static final String USERNAME = "root";
	public static final String PASSWORD = "123456";
	public static final String URL = "jdbc:mysql://localhost:3306/trainingdate";
	private static Connection conn = null;
	private static Statement stmt = null;
	private static ResultSet rs =null;
	
	public static void link() {
		try{
			Class.forName(DRIVER);
			conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
			stmt = conn.createStatement();
			String majorname = AddTrainingPlanForm.textField_4.getText();
			String majorid = AddTrainingPlanForm.textField_5.getText();
			String trainingpurpose = AddTrainingPlanForm.textField_6.getText();
			String trainingcontent = AddTrainingPlanForm.textField_7.getText();
			String classhours = AddTrainingPlanForm.textField_8.getText();
			String teacher = AddTrainingPlanForm.textField_9.getText();
			String sql = "INSERT INTO majorplan values ('"+majorname+"','"+majorid+"','"+trainingpurpose+"','"+trainingcontent+"','"+classhours+"','"+teacher+"')";
			
		
			stmt.executeUpdate(sql);
			
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		finally{
			try{
				if(rs!=null){
					rs.close();
				}
				if(stmt!=null){
					stmt.close();
				}
				if(conn!=null){
					conn.close();
				}
			}
			catch(SQLException ex){
				ex.printStackTrace();
			}
		}
	
	}	

	public static void save() {
		try{
			Class.forName(DRIVER);
			conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
			stmt = conn.createStatement();
			String name = AddTrainingPlanForm.textField_2.getText();
			String planyear = AddTrainingPlanForm.textField.getText();
			String startdate = AddTrainingPlanForm.textField_1.getText();
			String enddate = AddTrainingPlanForm.textField_3.getText();
			
			String sql = "INSERT INTO trainingplandto values ('"+name+"','"+planyear+"','"+startdate+"','"+enddate+"')";
			stmt.executeUpdate(sql);
			
			
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		finally{
			try{
				if(rs!=null){
					rs.close();
				}
				if(stmt!=null){
					stmt.close();
				}
				if(conn!=null){
					conn.close();
				}
			}
			catch(SQLException ex){
				ex.printStackTrace();
			}
		}
	
	}	


}


